//
//  BFShMineViewController.m
//  BeautyFood
//
//  Created by imac on 2020/4/27.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THMineViewController.h"
#import "THMyUserInfoViewController.h"
#import "THApplyResultViewController.h"
#import "THMineEarningsViewController.h"
#import "THMineCollectViewController.h"
#import "THAddressHomeViewController.h"
#import "BFShOrderHomeViewController.h"
#import "THSettingHomeViewController.h"
#import "THMineGroupViewController.h"
#import "THApplyToBeBabyTeacherViewController.h"
#import "THMineBabyHomeViewController.h"
#import "LoadViewController.h"
#import "THApplyNewBabyInfoViewController.h"
#import "THMineScoreViewController.h"
#import "THMineScoreViewController.h"
@interface THMineViewController()
@property(nonatomic, strong) STButton                     *iconButton;/**<  */
@property(nonatomic, strong) STLabel                      *nickLable;/**<  */
@property(nonatomic, strong) STLabel                      *idLable;/**<  */
@property(nonatomic, strong) UIImageView                          *codeImageView;/**<  */
@property(nonatomic, strong) THUserModel                     *userModel;/**<  */
@property(nonatomic, strong) NSMutableArray<NSArray*>                     *dataSouce;/**<  */
@property(nonatomic, strong) NSString                     *phone;/**<  */
@property(nonatomic, strong) NSArray                     *numberArray;/**<  */

@property(nonatomic, strong) NSString                     *score;/**<  */
@property(nonatomic, strong) NSString                     *money;/**<  */
@end
@implementation THMineViewController
#pragma mark --vc生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.userModel = STTools_UserManger.defult.stoneobs_fun_get_loginedUser;
    [self stoneobs_ui_configSubView];
    [self stoneobs_ui_configTableHeader];
    [self config_dataSouce];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [THUserDataController stoneobs_fun_send_get_user_ifno_request_id:@"" handle:^(THUserModel * _Nonnull user) {
        self.userModel = user;
        [STTools_UserManger.defult stoneobs_fun_updateUserModel:user];
        [self stoneobs_ui_configTableHeader];
    }];
    [self.view bringSubviewToFront:self.tableView];
    
    [BFBaseConfigDataController stoneobs_fun_get_customer_service_with_handle:^(NSString * _Nonnull phone, NSString * _Nonnull wechat) {
        self.phone = phone;
        [self config_dataSouce];
    }];
    
    [STTools_LocationManger.defult stoneobs_fun_st_startUpdatingLocationHande:^(STTools_Error *error, CLPlacemark *placemark) {
        [STTools_LocationManger.defult stoneobs_fun_st_updatePlacemarkWith:placemark];
        [THUserDataController updatePlace:STTools_LocationManger.defult.latitude lng:STTools_LocationManger.defult.longitude handle:^(NSError *error, BOOL success, NSDictionary *resp) {
            
        }];
    }];
    
    [BFBaseConfigDataController stoneobs_fun_get_qn_token_handle:^(NSString * _Nonnull qntoken, NSString * _Nonnull headurl) {
        STTools_UserManger.defult.qnToken = qntoken;
        STTools_UserManger.defult.qNhead = headurl;
    }];
    
    [THMineScoreViewController send_get_score_handle:^(NSString * _Nonnull score) {
        self.score = score;
        [self.tableView reloadData];
    }];

    
    [self send_get_num_request];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)config_dataSouce{
    self.dataSouce = NSMutableArray.new;
    NSString * money = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.money;
    
    ({
       NSArray * array = @[
           ({
               STTools_TableEasyModel * model = [[STTools_TableEasyModel alloc] initWithTextString:@"我的收益"  imageName:@"收益"];
               model.detailString = money;
                                    
               model;
           }),
           
           ({
               STTools_TableEasyModel * model = [[STTools_TableEasyModel alloc] initWithTextString:@"我的积分"  imageName:@"收益"];
               model.detailString = self.score;
                                    
               model;
           }),
           [[STTools_TableEasyModel alloc] initWithTextString:@"我的收藏" imageName:@"收藏-1"],
           [[STTools_TableEasyModel alloc] initWithTextString:@"收货地址" imageName:@"收货地址"],
           [[STTools_TableEasyModel alloc] initWithTextString:@"我的团队" imageName:@"团队"],
           ({
               STTools_TableEasyModel * model = [[STTools_TableEasyModel alloc] initWithTextString:@"联系客服" imageName:@"客服"];
               model.detailString = self.phone;
               model;
           }),

           [[STTools_TableEasyModel alloc] initWithTextString:@"设置" imageName:@"设置"],
           [[STTools_TableEasyModel alloc] initWithTextString:@"育婴师" imageName:@"设置"],
       ];
        [self.dataSouce addObject:array];
    });
    
    [self.tableView reloadData];
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    self.navigationController.navigationBar.hidden = YES;
    __weak typeof(self) weakSelf =  self;
    
    UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH , 192+TMUtils.stoneobs_fun_get_navgationBarBootom-64 + 40)];
    stoneobs_ui_imageView.image = [UIImage imageNamed:@"我的-bg"];
    [self.view addSubview:stoneobs_ui_imageView];
    stoneobs_ui_imageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf pushViewController:THMyUserInfoViewController.new animated:1];
    }];
    [stoneobs_ui_imageView addGestureRecognizer:tap];
    
    
    UIImageView * stoneobs_ui_s_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 14, 14)];
    stoneobs_ui_s_imageView.image = [UIImage imageNamed:@"椭圆1092"];
    [stoneobs_ui_imageView addSubview:stoneobs_ui_s_imageView];
    stoneobs_ui_s_imageView.right = UISCREEN_WIDTH  - 15;
    
    
    
    STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(20, TMUtils.stoneobs_fun_get_navgationBarBootom, 64, 64)
                                                              title:nil
                                                         titleColor:nil
                                                          titleFont:0
                                                       cornerRadius:32
                                                    backgroundColor:nil
                                                    backgroundImage:PLACE_HOLDER_IMAGE_1
                                                              image:nil];
    [self.view addSubview:stoneobs_ui_button];
    [stoneobs_ui_button stbw_setBorderWith:1 borderColor:[UIColor.whiteColor colorWithAlphaComponent:0.9] cornerRadius:32];
    
    self.iconButton = stoneobs_ui_button;
    stoneobs_ui_s_imageView.centerY = stoneobs_ui_button.centerY;
    [stoneobs_ui_button setClicAction:^(UIButton *sender) {

    }];
    [self.view bringSubviewToFront:self.tableView];
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(stoneobs_ui_button.right+15, 0, 200, 30)
                                                            text:@"sksks"
                                                       textColor:UIColor.whiteColor
                                                            font:18
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [self.view addSubview:stoneobs_ui_label];
    stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:18];
    stoneobs_ui_label.top = self.iconButton.top+3;
    self.nickLable = stoneobs_ui_label;
    

    STLabel * stoneobs_ui_n_label = [[STLabel alloc] initWithFrame:CGRectMake(stoneobs_ui_button.right+15, 0, 200, 30)
                                                            text:@"sksks"
                                                       textColor:UIColor.whiteColor
                                                            font:13
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [self.view addSubview:stoneobs_ui_n_label];
    stoneobs_ui_n_label.bottom = self.iconButton.bottom - 5;
    self.idLable = stoneobs_ui_n_label;
    
    
    UIImageView * codeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    codeImageView.contentMode = UIViewContentModeScaleAspectFill;
    codeImageView.clipsToBounds = YES;
    [self.view addSubview:codeImageView];
    codeImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * codeTap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        STTools_WebViewController * webView =  [STTools_WebViewController.alloc initWithUrl:weakSelf.userModel.QrCode];
        webView.forceTitle = @"二维码";
        [weakSelf pushViewController:webView animated:1];
    }];
    [codeImageView addGestureRecognizer:codeTap];
    codeImageView.right = UISCREEN_WIDTH - 35;
    codeImageView.centerY = self.iconButton.centerY;
    self.codeImageView = codeImageView;
    
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.frame = CGRectMake(0, stoneobs_ui_imageView.bottom - 65, STONEOBS_UI_UISCREEN_WIDTH, TMUtils.stoneobs_fun_get_tabBarTop - stoneobs_ui_imageView.bottom + 65 );

    [self.view addSubview:self.tableView];
    self.view.backgroundColor = self.tableView.backgroundColor;
    self.tableView.backgroundColor = UIColor.clearColor;
    
}
- (void)stoneobs_ui_configTableHeader{

    [self.codeImageView sd_setImageWithURL:self.userModel.QrCode.mj_url];
    [self.iconButton sd_setBackgroundImageWithURL:self.userModel.header_img.mj_url forState:UIControlStateNormal];
    self.nickLable.text = self.userModel.name;
    self.idLable.text = [NSString stringWithFormat:@"手机号:%@",self.userModel.phone];
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    
    CGFloat top = 0;
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, top, STONEOBS_UI_UISCREEN_WIDTH - 0, 88)];
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 20;
    view.clipsToBounds = YES;
    [header addSubview:view];

    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(16, 10, 200, 30)
                                                            text:@"我的订单"
                                                       textColor:UIColor.blackColor
                                                            font:15
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [header addSubview:stoneobs_ui_label];
    stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:18];
    top = stoneobs_ui_label.bottom  +10;
    ({

        
        
        NSArray * array = @[
            
            @{@"title":@"待付款",@"imageName":@"待付款"},
            @{@"title":@"待发货",@"imageName":@"待发货"},
            @{@"title":@"待收货",@"imageName":@"待收货"},
            @{@"title":@"已完成",@"imageName":@"已完成"},
            @{@"title":@"全部",@"imageName":@"全部"},
        ];
        CGFloat buttonWith = view.width / array.count;
        __weak typeof(self) weakSelf =  self;
        for (NSInteger i = 0 ;i < array.count;i ++) {
            NSNumber * number = self.numberArray[i];
            NSDictionary * dic = array[i];
            NSString * imageName = dic[@"imageName"];
            NSString * title = dic[@"title"];
            STTools_MenuControl * menuControl = [[STTools_MenuControl alloc] initWithFrame:CGRectMake(0, top, buttonWith, 53) imageName:imageName title:title titleColor:STONEOBS_UI_SECEND_TEXT_COLOR imageHeight:40 lableHeight:25];
            [menuControl setOnSelctedControl:^(STTools_MenuControl *control) {
                [weakSelf stoneobs_action_on_selcted_middle_menu_with_title:title];
            }];
            [view addSubview:menuControl];
            menuControl.left = (i % array.count) * buttonWith;
            view.height  = menuControl.bottom + 45;
            
            STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 16, 16)
                                                                      title:number.description
                                                                 titleColor:UIColor.whiteColor
                                                                  titleFont:8
                                                               cornerRadius:8
                                                            backgroundColor:FlatRed
                                                            backgroundImage:nil
                                                                      image:nil];
            stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            stoneobs_ui_button.hidden = (number.intValue == 0);
            [view addSubview:stoneobs_ui_button];
            stoneobs_ui_button.right = menuControl.right - 20;
            stoneobs_ui_button.top =  menuControl.top;
        }
    });
    
    
    header.clipsToBounds = YES;
    header.height = view.bottom - 20;

    self.tableView.tableHeaderView = header;
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    STTools_TableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    [cell setSt_tableEasyModel:model];
    cell.detailTextLabel.textColor = WH_UICOLOR_FROM_RGBA(0x0078FF);
    cell.accessoryView = [UIImageView.alloc initWithImage:ST_IMAGE_NAMED(@"椭圆1092")];
    
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTools_TableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    if ([model.textString isEqualToString:@"我的收益"]) {
        [self pushViewController:THMineEarningsViewController.new animated:1];
    }
    if ([model.textString isEqualToString:@"我的积分"]) {
        [self pushViewController:THMineScoreViewController.new animated:1];
    }
    if ([model.textString isEqualToString:@"我的收藏"]) {
        [self pushViewController:THMineCollectViewController.new animated:1];
    }
    if ([model.textString isEqualToString:@"收货地址"]) {
        [self pushViewController:THAddressHomeViewController.new animated:1];
    }
    if ([model.textString isEqualToString:@"我的团队"]) {
        [self pushViewController:THMineGroupViewController.new animated:1];
    }
    if ([model.textString isEqualToString:@"设置"]) {
        [self pushViewController:THSettingHomeViewController.new animated:1];
//        [self pushViewController:LoadViewController.new animated:1];
        
    }
    if ([model.textString isEqualToString:@"联系客服"]) {
        NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",model.detailString];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:nil completionHandler:^(BOOL success) {
            
        }];
    }
    
    if ([model.textString isEqualToString:@"育婴师"]) {
        [THBabyTeacherDataController send_get_apply_ino_request:^(THApplyBabyTeacherModel * _Nonnull model) {
            if (model.real_name.length < 1) {
                //没有申请过
                THApplyNewBabyInfoViewController* vc =  THApplyNewBabyInfoViewController.new;
                vc.apply_model = model;
                [self pushViewController:vc animated:1];
            }else{
                if (model.status.intValue == 1) {
                    //去 我的育婴师界面
                    [self pushViewController:THMineBabyHomeViewController.new animated:1];
                }else{
                    //去其他界面
                    THApplyResultViewController* vc =  THApplyResultViewController.new;
                    vc.apply_model = model;
                    [self pushViewController:vc animated:1];
                }

                
            }

        }];
        
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --stoneobs Action Method
- (void)stoneobs_action_on_selcted_middle_menu_with_title:(NSString*)title{

        BFShOrderHomeViewController * vc = BFShOrderHomeViewController.new;
        if ([title isEqualToString:@"全部"]) {
            vc.selcted_index = 5;
        }
        if ([title isEqualToString:@"待付款"]) {
            vc.selcted_index = 0;
        }
        if ([title isEqualToString:@"待发货"]) {
          vc.selcted_index = 1;
        }
        if ([title isEqualToString:@"待收货"]) {
            vc.selcted_index = 2;
        }
        if ([title isEqualToString:@"已完成"]) {
            vc.selcted_index = 3;
        }
    
        [self pushViewController:vc animated:1];
}
- (void)send_get_num_request{

    [BFShOrderDataController stoneobs_fun_send_get_number_handle:^(NSString * _Nonnull no_pay, NSString * _Nonnull no_exp, NSString * _Nonnull no_get) {
        self.numberArray = @[@(no_pay.intValue),@(no_exp.intValue),@(no_get.intValue),@(0),@(0)];
        [self stoneobs_ui_configTableHeader];
    }];
}

@end
